<?php
class DataReportAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'tpl';
		$this->layout = 'admin';
	}

	public function execute()
	{
		$type	= $this->context->get('type');
		$this->data['type']	= $type;
		$this->data['types']= array(1=>'最新浏览',2=>'最新点击',3=>'最新购买',4=>'热门浏览',5=>'热门点击',6=>'热门购买',);
		$viewHistoryService	= new ViewHistoryService();
		if(empty($type))
		{
			$du			= C('DateUtil');
			$yestoday	= date('Y-m-d',$du->dateAdd('d','-1',time()));
			$this->data['summary']['view']	= $viewHistoryService->getCountByDate($yestoday,1);
			$this->data['summary']['click']	= $viewHistoryService->getCountByDate($yestoday,2);
			$this->data['summary']['buy']	= $viewHistoryService->getCountByDate($yestoday,3);
		}
		else
		{
			$con		= array();
			$orderby	= '';
			//类型
			switch($type)
			{
				case 1:
				case 4:
					$con['type']	= 1;
					break;
				case 2:
				case 5:
					$con['type']	= 2;
					break;
				case 3:
				case 6:
					$con['type']	= 3;
					break;
			}
			//排序
			switch($type)
			{
				case 1:
				case 2:
				case 3:
					$orderby	= 'updated DESC';
					break;
				case 4:
				case 5:
				case 6:
					$orderby	= 'view_num DESC';
					break;
			}
			$page		= $this->getPage();
			$tmp	 	= LtObjectUtil::singleton('LtConfig')->get('pagination.pager');
			$pageSize	= $tmp['per_page'];
			if (empty($pageSize))
			{
				$pageSize 	= 20;
			}
			$num 		= $pageSize;
			$start	 	= ($page-1) * $pageSize;
			$data		= $viewHistoryService->getList($con,$orderby, $start, $num);
			$tmp		= C('ViewHistoryUtil')->getProductList($data,'num_iid,title',0);
			
			if($tmp)
			{
				$tmp2	= array();
				foreach($tmp as $tv)
				{
					$tmp2['id'.$tv['num_iid']]	= $tv;
				}
				
				$confBaseDir	= substr(dirname(__FILE__), 0, strrpos(__FILE__, "admin")).'frontend/conf/standard/';
				$routerFile		= $confBaseDir.'router.conf.php';
				$content		= file_get_contents($routerFile);
				
				if(preg_match('~\'protocol\'\s+=>\s+\'(.*?)\'~is',$content,$m))
				{
					$protocol	= $m[1];
				}
				if(preg_match('~\'postfix\'\s+=>\s+\'(.*?)\'~is',$content,$m))
				{
					$postfix	= $m[1];
				}
				
				if($protocol=='rewrite')
				{
					$url	= $this->data['baseurl'].'Default-View-id-';
				}
				else if($protocol=='path_info')
				{
					$url	= $this->data['baseurl'].'index.php/Default-View-id-';
				}
				else
				{
					$url	= $this->data['baseurl'].'index.php?module=Default&action=View&id=';
				}
				
				foreach($data['rows'] as &$dv)
				{
					$dv['title']	= $tmp2['id'.$dv['num_id']]['title'];
					if($protocol=='standard')
					{
						$dv['url']		= $url.$dv['num_id'];
					}
					else
					{
						$dv['url']		= $url.$dv['num_id'].$postfix;
					}
				}
			}
			$this->data['data'] = $data;
			
			$count 		= $this->data['data']['count'];
			$baseUrl 	= C('LtUrl')->generate('Data', 'Report', array('type'=>$type, 'p' => ':page'));
			
			$pagination = new LtPagination;
			$pagination->init();
			$this->data['pages'] = $pagination->pager($page, $count, $baseUrl);
		}
		
		
		$this->data['menu']	= 'default';
	}
}
